# Maintainer: Stephen Groat <stephen@egroat.com>

_realname=cmocka
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.7
pkgrel=1
pkgdesc="Unit testing framework for C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:Apache-2.0')
url="https://cmocka.org/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://cmocka.org/files/1.1/${_realname}-${pkgver}.tar.xz"
        "001-install-fixes.patch"
        "002-dll-name.patch"
        "003-fix-static-libname.patch"
        "004-fix-pkgconfig-files.patch")
sha256sums=('810570eb0b8d64804331f82b29ff47c790ce9cd6b163e98d47a4807047ecad82'
            '1017cce7912dfb485c372b70cc7c9e620d3fb968bb417a9557e2a79363ac8a3f'
            'd5fdbcdd5cd2abc7b275a5b57f529dfa0edebe700fe1e85fafd5620fc8465a1f'
            '3250e4752282d62d5eeaf789f87fdaa6737e0c583a18b06117c7623d410f8779'
            '945d5ae4fb43712c3de2ba441d6e8394a4e5f15e6aaadbb85e2e9bd5dc8a915a')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/001-install-fixes.patch"
  patch -p1 -i "${srcdir}/002-dll-name.patch"
  patch -p1 -i "${srcdir}/003-fix-static-libname.patch"
  patch -p1 -i "${srcdir}/004-fix-pkgconfig-files.patch"
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_CMOCKERY_SUPPORT=ON \
    -DUNIT_TESTING=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd ${srcdir}/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
